package com.msh.starter.common.web.bean;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
import java.io.IOException;

/**
 * java Long 类型 js精度丢失问题
 */
public class JsLongAccuracySerializer  extends StdScalarSerializer<Long> {
  private static final long JS_MAX_NUM = (1L<<53 ) - 1;
  public final static JsLongAccuracySerializer instance = new JsLongAccuracySerializer();

  private JsLongAccuracySerializer() {
    super(Long.class);
  }


  @Override
  public void serialize(Long value, JsonGenerator gen, SerializerProvider provider)
      throws IOException {
    if(value > JS_MAX_NUM){
      gen.writeString(value.toString());
    }else {
      gen.writeNumber(value.longValue());
    }
  }
}
